home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5533 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  75 lines

  1. Path: news.compuserve.com!newsmaster
  2. From: 76623,2065@compuserve.com  (Bobby Martin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: a question about abstract base classes and libraries
  5. Date: 5 Feb 1996 14:55:13 GMT
  6. Organization: CompuServe Incorporated
  7. Message-ID: <4f55oh$mu5@dub-news-svc-4.compuserve.com>
  8. References: <4es0b1$au7@news.sdd.hp.com>
  9. Reply-To: 76623,2065@compuserve.com (Bobby Martin)
  10. NNTP-Posting-Host: dd36-155.compuserve.com
  11. X-Newsreader: IBM NewsReader/2 v1.03
  12.  
  13. In <4es0b1$au7@news.sdd.hp.com>, Laura Mansfield <laura> writes:
  14. >I'm writing a library, lib.a, to be linked to by some client code that
  15. ..
  16. >
  17. >I'd like to hide the private data members of this class, and so have
  18. >decided to create and abstract base class, and make visible to the client
  19. >code only the derived class. To better explain, here's an example:
  20. >
  21. >In my library code:
  22. >
  23. >class Abstract
  24. >{
  25. >
  26. >  protected:
  27. >
  28. >  int a;
  29. >  int b;
  30. >
  31. >  public:
  32. >
  33. >  virtual void func( void ) = 0;
  34. >};
  35. >
  36. >In lib.h:
  37. >
  38. >class ClassA : public abstract
  39. >{
  40. >
  41. >  public:
  42. >
  43. >  virtual void func( void );
  44. >};
  45. >
  46. >But I'm beginning to realize I can't really do this w/out defining
  47. >class Abstract in lib.h.  I tried to do "class Abstract;" at the beginning
  48. >of lib.h, but I think this only works for pointers.
  49. >
  50. >Can anyone help me w/ what to do here?
  51. >
  52. >Laura
  53.  
  54. I just thought of this while reading your post, so I haven't tried it, but the
  55. following should work: 
  56.  
  57. make abstract a member variable:
  58.  
  59. class ClassA
  60.     {
  61. private:
  62. ..
  63.     abstract* holdsAllData;
  64. ..
  65.     }
  66. and create a new one with 'holdsAllData = new abstract' in each ClassA
  67. constructor.  Then when you need to access ClassA data, use a format such as
  68.  
  69. holdsAllData->a;
  70.  
  71. Then you only need to #include abstract in ClassA's implementation file, not
  72. in the header.  A forward declaration will be sufficient there.
  73.  
  74. PS Don't forget to delete holdsAllData in the destructor : )
  75.